//userCenter.js
const util = require("../../../utils/util.js");

//获取应用实例
var app = getApp()
Page({
  data: {
    // 旧密码
    oldPassword: "",
    // 新密码
    newPassword: "",
    // 确认密码
    confirmPassword: ""
  },
  onLoad: function () {
    this.$wuxToast = app.wux(this).$wuxToast;
  },
  // 旧密码
  inputOldPassword: function (e) {
    this.setData({
      oldPassword: e.detail.value
    })
  },
  // 新密码
  inputNewPassword: function (e) {
    this.setData({
      newPassword: e.detail.value
    })
  },
  // 确认密码
  inputConfirmPassword: function (e) {

    this.setData({
      confirmPassword: e.detail.value
    })
  },
  // 确认密码 光标离开
  blurConfirmPassword: function(e) {
    var newPassword = this.data.newPassword;
    if (newPassword != e.detail.value) {
      this.$wuxToast.show({
        type: 'text',
        timer: 1000,
        color: '#fff',
        text: '两次密码输入不一致，请重新输入',
        success: () => console.log('两次密码输入不一致，请重新输入')
      })
      this.setData({
        newPassword: "",
        confirmPassword: ""
      })
    }
  },
  // 修改密码
  updateBtnAction: function () {
    console.log("logout");

    var oldPasswordStr = this.data.oldPassword;
    var newPasswordStr = this.data.newPassword;
    var confirmPasswordStr = this.data.confirmPassword;
    if (oldPasswordStr == "") {
      this.$wuxToast.show({
        type: 'text',
        timer: 1000,
        color: '#fff',
        text: '旧密码不能为空',
        success: () => console.log('旧密码不能为空')
      });
      return;
    }
    if (newPasswordStr == "") {
      this.$wuxToast.show({
        type: 'text',
        timer: 1000,
        color: '#fff',
        text: '新密码不能为空',
        success: () => console.log('新密码不能为空')
      });
      return;
    }
    if (newPasswordStr != confirmPasswordStr) {
      this.$wuxToast.show({
        type: 'text',
        timer: 1000,
        color: '#fff',
        text: '两次密码输入不一致，请重新输入',
        success: () => console.log('两次密码输入不一致，请重新输入')
      })
      this.setData({
        confirmPassword: ""
      })
      return;
    }

    //加载提示框
    util.showLoading();
    var that = this;
    var urlString = app.globalData.BaseURL + '/api/adminUser/updatePassword';

    var dataSource = util.json2Form({
      oldPassword: oldPasswordStr,
      newPassword: newPasswordStr
    });

    wx.request({
      method: "POST",
      url: urlString,
      data: dataSource,
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'adminUserViewId': app.globalData.adminUserViewId,
        'token': app.globalData.token
      },
      success: function (res) {
        util.hideToast();
        console.log("data=====" + res.data);
        var code = res.data.code;
        console.log("code=====" + code);
        // code = 300;
        if (code === 200) {
          that.$wuxToast.show({
            type: 'text',
            timer: 1000,
            color: '#fff',
            text: '修改成功',
            success: () => console.log('修改成功')
          });
          setTimeout(function () {
            wx.navigateBack({});
          }, 1000);
        } else {
          that.$wuxToast.show({
            type: 'text',
            timer: 1000,
            color: '#fff',
            text: '修改失败，请重新提交。',
            success: () => console.log('修改失败，请重新提交。')
          });
        }
      },
      fail: function () {
        that.$wuxToast.show({
          type: 'text',
          timer: 1000,
          color: '#fff',
          text: '网络错误，请稍后重试。',
          success: () => console.log('网络错误，请稍后重试。')
        })
      }, complete: function () {
        util.hideToast();
      }
    })

   

  }
})